package com.dux.spbtservice.service;

import com.dux.spbtservice.domin.Perm;
import com.dux.spbtservice.domin.User;
import com.dux.spbtservice.jpa.PermRepo;
import com.dux.spbtservice.jpa.RoleRepo;
import com.dux.spbtservice.jpa.UserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserRepo userRepo;

    @Autowired
    private RoleRepo roleRepo;

    @Autowired
    private PermRepo permRepo;

    public User getUser(String username){
        return userRepo.findByUsername(username);
    }

    public List<Perm> perms(List<Long> roleId){
        List<Perm> perms = new ArrayList<>();
        roleRepo.findAllById(roleId).forEach(role -> {
            if (!CollectionUtils.isEmpty(role.getPerms())){
                perms.addAll(role.getPerms());
            }
        });
        return perms;
    }
    public List<Perm> perms(){
        return permRepo.findAll();
    }
}
